package com.dfbz.dao;


import com.dfbz.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

@Repository
public interface LoginDao extends Mapper<User> {

    @SelectProvider(value = LoginSqlProvider.class,method = "findByUser")
    User findByUser(String username);


    @SelectProvider(value = LoginSqlProvider.class,method ="register")
    void register(User user);

    @Select("update xb.user \n" +
            "set `password`= #{newPassWord} \n" +
            "where email = #{userEmail}")
    void save(@Param("newPassWord") String newPassWord,@Param("userEmail") String userEmail);

    @SelectProvider(value = LoginSqlProvider.class,method = "findByEmailCount")
    Integer findByEmailCount(String email);

    @SelectProvider(value = LoginSqlProvider.class,method = "findUserInfo")
    Map<Object,Object> findUserInfo(Object userId);


    /**
     * 条件分页逻辑
     * @param params 条件map
     * @return 对象集合
     */
    @SelectProvider(value = LoginSqlProvider.class,method = "selectPageCondition")
    List<User> selectPageCondition(Map<String, String> params);

    @Select(value ="select d.id\n" +
            "from dept d\n" +
            "left join user u on u.dept_name=d.`name`\n" +
            "where u.id=#{id}")
    int deptNameUpdataId(@Param("id") Integer id);

    @Select(value ="update user as u\n" +
            "set u.dept_id=#{dbUserDeptId}\n" +
            "where u.id=#{id}")
    void updateUserId(@Param(value = "dbUserDeptId") int dbUserDeptId
            ,@Param(value = "id")int id);


    /**
     * 找我关注 的用户真实姓名
     * @param id 登录用户id
     * @return 关注用户的真实姓名 列表
     */
    @Select(value="select u.real_name\n" +
            "from user u \n" +
            "where u.id in (select uf.user_focus_id\n" +
            "from userfocus uf\n" +
            "where uf.user_id=#{id})\n")
    List<User> findFollowRealName(@Param(value="id") Integer id);


    @Select(value = "select u.id,u.real_name\n" +
            "from user u     \n" +
            "where u.id in \n" +
            "(select uf.user_focus_id    \n" +
            "from userfocus uf    \n" +
            "where uf.user_id=#{userId}) \n" +
            "and u.id in \n" +
            "(select f.u_id from favorite f where f.a_id=#{articleId})")
    List<User> followArticleMyFriend(@Param(value = "articleId") String articleId, @Param(value = "userId") Integer userId);


    /**
     * 找用户表 给部门id分组
     * @return dept_id(部门id)、deptName(部门名)、total(部门用户数)
     */
    @Select(value = "select u.dept_id,count(1) as total,\n" +
            "(select d.`name` from dept d where d.id=u.dept_id) as deptName\n" +
            "from user u\n" +
            "group by u.dept_id")
    List<Map<String,Object>> findAllDept();

    /**
     *
     * @return
     */
    @Select(value = "select u.real_name,u.id\n" +
            "from user u\n" +
            "where u.dept_id=#{deptId}")
    List<Map<String, String>> findDeptRealName(@Param(value = "deptId") String deptId);
}
